/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.gui.session.utils;

import net.zarubsys.robotplayground.gui.session.plugin.SessionGUIPlugin;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;

/**
 * ExistingFileSelectionPage
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class ExistingFileSelectionPage extends WizardPage {

	private IFile result = null;
	private Text path;
	private final String resourceName;
	private final String extension;

	public ExistingFileSelectionPage(String pageName, String resourceName, String extension) {
		super(pageName);
		this.resourceName = resourceName;
		this.extension = extension;
	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout(2, false));
		
		GridData gd = new GridData();
		gd.horizontalSpan = 2;
		Label label = new Label(composite, SWT.NONE);
		label.setText("Please select " + resourceName + " to be used");
		label.setLayoutData(gd);
		
		path = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		path.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		
		Button browseButton = new Button(composite, SWT.NONE);
		browseButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
		browseButton.setText("Browse");
		browseButton.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 
						new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
				dialog.setTitle("Select " + resourceName);
				dialog.setMessage("Select " + resourceName + " from the tree");
				dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
				dialog.setAllowMultiple(false);
				dialog.setValidator(new ISelectionStatusValidator() {
					
					@Override
					public IStatus validate(Object[] selection) {
						 if(selection.length == 1) {
                             if(selection[0] instanceof IFile) {
                                   IFile file = (IFile) selection[0];
                                   return new Status(IStatus.OK, SessionGUIPlugin.PLUGIN_ID,
                                     IStatus.OK, resourceName + " " + file.getName() + " selected", null);
                             }
                       }
                       return new Status(IStatus.ERROR, SessionGUIPlugin.PLUGIN_ID,
                         IStatus.ERROR, "Selection must be a " + resourceName + " file", null);
					}
					
				});
				dialog.addFilter(new ViewerFilter() {
					
					@Override
					public boolean select(Viewer viewer, Object parentElement, Object element) {
						if (!(element instanceof IFile)) return true;
						IFile f = (IFile) element;
						String fileExtension = f.getFullPath().getFileExtension();
						return extension.equals(fileExtension);
					}
				});
				if (dialog.open() != Window.OK) return;
				Object dialogResult = dialog.getResult()[0];
				Assert.isLegal(dialogResult instanceof IFile, "Expected result of IFile! Got " + dialogResult);
				ExistingFileSelectionPage.this.result = (IFile) dialogResult;
				path.setText(ExistingFileSelectionPage.this.result.getFullPath().toString());
				validate();
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// ignored
			}
			
		});
		
		setControl(composite);
		validate();
	}
	
	private void validate() {
		if (result == null) {
			setErrorMessage("Please select existing " + resourceName);
			setPageComplete(false);
			return;
		} 
		setErrorMessage(null);
		setPageComplete(true);
	}
	
	public IFile getResult() {
		return result;
	}


}

